﻿using AutoMapper;
using B.S.BaseData.Domain;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure;
using B.S.BaseData.Read.API.Application.Command;
using MediatR;

namespace B.S.BaseData.Read.API.Application.CommandHandler
{
    /// <summary>
    /// 商品查询命令处理
    /// </summary>
    public class GoodsTypeQueryCommandHandler : IRequestHandler<GoodsTypeQueryCommand, ApiResult<List<GoodsType>>>
    {

        /// <summary>
        /// 商品名称仓储
        /// </summary>
        private readonly IBaseRepository<GoodsType> goodstypeRepository;

        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper mapper;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="goodstypeRepository">商品名称仓储</param>
        /// <param name="mapper">映射</param>
        public GoodsTypeQueryCommandHandler(IBaseRepository<GoodsType> goodstypeRepository, IMapper mapper)
        {
            this.goodstypeRepository = goodstypeRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns></returns>
        public Task<ApiResult<List<GoodsType>>> Handle(GoodsTypeQueryCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<GoodsType>> result = new ApiResult<List<GoodsType>>();
            result.Code = ApiEnums.查询成功;
            result.Msg = ApiEnums.查询成功.ToString();
            var list = goodstypeRepository.GetAll();
            result.Data = list.ToList();
            return Task.FromResult(result);
        }
    }
}
